/************************************************************************************/
/*  Program name CAPGLEXT_BC_PACKET.sql                                             */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET VERIFY OFF
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLEXT_BC_PACKET';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPGL_BC_PACKET.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_B (p_NGO VARCHAR2) IS
	SELECT
		F.*
	FROM
		GL_SETS_OF_BOOKS	B,
		GL_BC_PACKETS		F
	WHERE	SUBSTR(B.Short_Name,5,4)	=	p_NGO
	AND		B.Set_Of_Books_ID	=	F.Set_Of_Books_ID
	ORDER BY
		F.Packet_ID;


BEGIN
	lv_NGO		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	GL_SETS_OF_BOOKS
	WHERE	SUBSTR(Short_Name,5,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_B(lv_NGO) LOOP

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Packet_ID||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.JE_Source_Name||CHR(9)||
				C1.JE_Category_Name||CHR(9)||
					C1.Code_Combination_ID||CHR(9)||
				C1.Actual_Flag||CHR(9)||
				C1.Period_Name||CHR(9)||
				C1.Period_Year||CHR(9)||
				C1.Period_Num||CHR(9)||
				C1.Quarter_Num||CHR(9)||
				C1.Currency_Code||CHR(9)||
				C1.Status_Code||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Budget_Version_ID||CHR(9)||
					C1.Encumbrance_Type_ID||CHR(9)||
				C1.Template_ID||CHR(9)||
				C1.Entered_DR||CHR(9)||
				C1.Entered_CR||CHR(9)||
				C1.Accounted_DR||CHR(9)||
				C1.Accounted_CR||CHR(9)||
				C1.USSGL_Transaction_Code||CHR(9)||
				C1.Originating_RowID||CHR(9)||
				C1.Account_Segment_Value||CHR(9)||
				C1.Automatic_Encumbrance_Flag||CHR(9)||
					C1.Funding_Budget_Version_ID||CHR(9)||
				C1.Funds_Check_Level_Code||CHR(9)||
				C1.Amount_Type||CHR(9)||
				C1.Boundary_Code||CHR(9)||
				C1.Tolerance_Percentage||CHR(9)||
				C1.Tolerance_Amount||CHR(9)||
				C1.Override_Amount||CHR(9)||
				C1.DR_CR_Code||CHR(9)||
				C1.Account_Type||CHR(9)||
				C1.Account_Category_Code||CHR(9)||
				C1.Effect_On_Funds_Code||CHR(9)||
				C1.Result_Code||CHR(9)||
				C1.Budget_Posted_Balance||CHR(9)||
				C1.Actual_Posted_Balance||CHR(9)||
				C1.Encumbrance_Posted_Balance||CHR(9)||
				C1.Budget_Approved_Balance||CHR(9)||
				C1.Actual_Approved_Balance||CHR(9)||
				C1.Encumbrance_Approved_Balance||CHR(9)||
				C1.Budget_Pending_Balance||CHR(9)||
				C1.Actual_Pending_Balance||CHR(9)||
				C1.Encumbrance_Pending_Balance||CHR(9)||
				C1.Reference1||CHR(9)||
				C1.Reference2||CHR(9)||
				C1.Reference3||CHR(9)||
				C1.Reference4||CHR(9)||
				C1.Reference5||CHR(9)||
				C1.JE_Batch_Name||CHR(9)||
				C1.JE_Batch_ID||CHR(9)||
				C1.JE_Header_ID||CHR(9)||
				C1.JE_Line_Num||CHR(9)||
				C1.JE_Line_Description||CHR(9)||
				C1.Reference6||CHR(9)||
				C1.Reference7||CHR(9)||
				C1.Reference8||CHR(9)||
				C1.Reference9||CHR(9)||
				C1.Reference10||CHR(9)||
				C1.Reference11||CHR(9)||
				C1.Reference12||CHR(9)||
				C1.Reference13||CHR(9)||
				C1.Reference14||CHR(9)||
				C1.Reference15||CHR(9)||
				CAPBREXT_COMMON.GetSOBShortName(C1.Set_Of_Books_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetEncumType(C1.Encumbrance_Type_ID)||CHR(9)||
				CAPBREXT_COMMON.GetBudgetName(C1.Funding_Budget_Version_ID)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such SOB exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/